<template>
  <div>
    brother
    <br />
    vuex中state的数据:{{ count }}
    <button @click="add">修改count</button>
    <button @click="add1">修改name</button>
    <br />
    延迟执行: <button @click="actions">异步执行</button>
    <br />
    数组:{{ $store.state.list }}
    <p>和:{{ sum }}</p>
    <p>平均:{{ (sum / $store.state.list.length).toFixed(2) }}</p>
    <p>最大值:{{ max }}</p>
    <p>最小值:{{ min }}</p>
    <p>正序:{{ zhengxu }}</p>
    <p>倒序:{{ daoxu }}</p>
    <p>
      飞机:{{ $store.state.feiji.obj.name }}==重量{{
        $store.state.feiji.obj.Weight
      }}
    </p>
    <p>
      火车:{{ $store.state.huoche.obj.name }} ==重量{{
        $store.state.huoche.obj.Weight
      }}
    </p>
    <button @click="change">修改重量</button>
  </div>
</template>
<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {

  name: 'brother',
  data () {
    return {
      uname: '天线'
    }
  },
  created () {
    console.log(this.$store.state);
  },
  computed: {
    //1.获取state中的数据,通过计算属性获取
    count () {
      return this.$store.state.count
    },
    ...mapGetters(['sum', 'max', 'min', 'zhengxu', 'daoxu'])

  },
  methods: {
    //2.修改state中的数据
    add () {
      this.$store.commit('addcount', parseInt(Math.random() * 10))
    },
    add1 () {
      this.$store.commit('changeName', this.uname)
    },
    actions () {
      console.log(this.$store);
      this.$store.dispatch('getTime', 20)
    },
    change () {
      this.$store.commit('feiji/changeWeight')
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
